import math

import get_data_from_database.database_connection as dbConnection
import numpy as np
from utils.date_utils.date_function import is_weekday_commit \
    , project_age, get_latency_after_response, get_waiting_time, get_close_pr_time
from utils.file_utils.file_function import get_file_directory_num, get_language_num, get_file_type_num, \
    get_segs_added_num, get_segs_deleted_num, get_segs_updated_num, get_subsystem_num, get_changes_files_modified_num, \
    get_file_developer_num, get_file_developer_change_num, get_file_developer_recent_change_num, get_test_inclusion
from utils.num_utils.num_function import get_label_count \
    , get_workload, get_prev_prs, get_change_num \
    , get_accept_num, get_close_num, get_review_num \
    , get_participants_count
from utils.path_exist import path_exists_or_create
from utils.str_utils.str_function import wordCount
from utils.num_utils.num_ratio_function import get_pr_author_rate \
    , get_project_line_rate, get_line_weekday_rate, get_project_line_churn_rate \
    , get_commits_average, get_avg_comments, get_avg_latency
import os
import pandas as pd
# 增加代码的可读性
pr_number_index = 0
repo_name_index = 1
pr_user_id_index = 2
pr_author_association_index = 3
# labels_index = 4
# created_at_index = 5
# closed_at_index = 6
# merged_at_index = 7
# merged_index = 8
# mergeable_state_index = 9
# assignees_content_index = 10
# comments_number_index = 11
# comments_content_index = 12
# review_comments_number_index = 13
# review_comments_content_index = 14
# commit_number_index = 15
# changed_file_num_index = 16
# total_add_line_index = 17
# total_delete_line_index = 18
# pr_user_name_index = 19
# pr_id_index = 20
# updated_at_index = 21
# content_index = 22
# title_index = 23
# body_index = 24
# commit_content_index = 25
created_at_index = 4
closed_at_index = 5
merged_at_index = 6
merged_index = 7
mergeable_state_index = 8
assignees_content_index = 9
comments_number_index = 10
comments_content_index = 11
review_comments_number_index = 12
review_comments_content_index = 13
commit_number_index = 14
changed_file_num_index = 15
total_add_line_index = 16
total_delete_line_index = 17
pr_user_name_index = 18
pr_id_index = 19
updated_at_index = 20
content_index = 21
title_index = 22
body_index = 23
commit_content_index = 24
def get_data_by_repo_name(repo_name):
    # 从数据库获取数据
    raw_data = dbConnection.getDataFromSql(
        "select * from pr_self where repo_name='" + repo_name + "' and created_at >= '2021-6-1 00:00:00' order by pr_number")
    #"select * from pr_self where repo_name='" + repo_name + "' and closed_at is not null order by pr_number")
    print(len(raw_data))  ##查看PR数量

    # 标记有用的PR自身信息的下标
    useful_features_index = [0,  ##pr_number
                             2,  ##repo_name
                             3,  ##pr_user_id
                             5,  ##pr_author_association
                             8,  ##labels
                             10,  ##created_at
                             12,  ##closed_at
                             13,  ##merged_at
                             14,  ##merged
                             16,  ##mergeable_state
                             18,  ##assignees_content
                             20,  ##comments_number
                             21,  ##comments_content
                             22,  ##review_comments_number
                             23,  ##review_comments_content
                             24,  ##commit_number
                             26,  ##changed_file_num
                             27,  ##total_add_line
                             28,  ##total_delete_line
                             4,  ##pr_user_name
                             11,  ##updated_at
                             6,  ##title
                             7,  ##body
                             25,  ##commit_content
                             ]

    selected_data = []  ##保留有用的属性特征

    for item in raw_data:
        tmp = []
        for i in useful_features_index:
            tmp.append(item[i])
        selected_data.append(tmp)

    pr_number_list = ''
    count = 0
    for item in selected_data:
        tmp = []
        ##pr_number
        tmp.append(item[0])
        pr_number_list = pr_number_list + str(item[0]) + ','
        count += 1

    print(count)
    print(
        "select pr_number,repo_name,changed_file_name,sha,changed_file_status,lines_added_num,lines_deleted_num,lines_changed_num,contain_patch,patch_content from pr_file " +
        "where repo_name='" + repo_name + "'  and pr_number in (" + pr_number_list[
                                                                    0:pr_number_list.__len__() - 1] + ") order by pr_number")
    ##获得文件数据
    file_data = dbConnection.getDataFromSql(
        "select pr_number,repo_name,changed_file_name,sha,changed_file_status,lines_added_num,lines_deleted_num,lines_changed_num,contain_patch,patch_content from pr_file " +
        "where repo_name='" + repo_name + "'  and pr_number in (" + pr_number_list[
                                                                    0:pr_number_list.__len__() - 1] + ") order by pr_number"
    )

    print("prfile搜索完成")
    file_dict = {}
    index = 0
    while index < file_data.__len__():
        if file_dict.__contains__(file_data[index][0]) is False:
            file_dict[file_data[index][0]] = {}
        file_dict[file_data[index][0]][file_data[index][2]] = {file_data[index][9]}
        index = index + 1
    # file_directory_num = get_file_directory_num(file_dict)
    # print("get_file_directory_num")
    # file_subsystem_num = get_subsystem_num(file_dict)
    # print("get_subsystem_num")
    # language_num = get_language_num(file_dict)
    # print("get_language_num")
    # file_type_num = get_file_type_num(file_dict)
    # print("get_file_type_num")
    file_data = pd.DataFrame(data=file_dict)
    file_data = (file_data.T)
    file_data.to_csv(repo_name + '.csv')
    file_segs_added_num = get_segs_added_num(file_dict)
    print("get_segs_added_num")
    print(file_segs_added_num)
    file_segs_deleted_num = get_segs_deleted_num(file_dict)
    print("get_segs_deleted_num")
    print(file_segs_deleted_num)
    file_segs_updated_num = get_segs_updated_num(file_dict)
    print("get_segs_updated_num")
    print(file_segs_updated_num)
    # changes_files_modified_num = get_changes_files_modified_num(file_dict)
    # print("get_changes_files_modified_num")

    for key, value in file_segs_updated_num.items():
        if key in file_segs_added_num:
            file_segs_updated_num[key] += file_segs_added_num.get(key)
        if key in file_segs_deleted_num:
            file_segs_updated_num[key] += file_segs_deleted_num.get(key)
    return file_segs_updated_num


if __name__ == '__main__':
    #repo_list = ["tensorflow", "dubbo", "Ipython", "Katello", "kuma", "moby", "opencv", "react", "scikit-learn", "terraform"]
    #repo_list = ["tensorflow"]
    repo_list = ["yii2", "netbeans", "kafka", "django", "phoenix"]
    for repo_name in repo_list:
        # repo_name = "tensorflow"  # "storm"#"scikit-learn"#"moby"#"cocos2d-x"#"netbeans"#"yii2"#"dubbo"#"react"#"tensorflow"#"opencv"#"phoenix"#"helix"#"terraform"#"Ipython"# "kuma"#"incubator-heron"#"kuma"#"incubator-heron"#"Katello"#"zipkin"#"yii2"

        file_path = "F:\\devops+\\评审仿真\\" + repo_name + "数据\\" + repo_name + ".csv"
        postfix = '.' + file_path.split('.')[-1]
        file_path = file_path[:file_path.find(postfix)]
        #F:\\devops+\\评审仿真\\tensorflow数据\\tensorflow_pr_mP.xlsx
        print(file_path)
        # file_segs_updated_num = get_data_by_repo_name(repo_name)
        # print(file_segs_updated_num)
        # df = pd.DataFrame(list(file_segs_updated_num.items()), columns=['pr_number', 'change_segments'])
        # print(df)
        # print(df.columns)
        # df.to_excel(f'{file_path}_pr_changeSeg.xlsx', index=False)
        # original = pd.read_excel(f'{file_path}_pr_mP.xlsx')
        # original = original.set_index('pr_number').join(df.set_index('pr_number'))
        # print(original)
        # original['change_segments'].fillna(0, inplace=True)
        # original.to_excel(f'{file_path}_pr_mP_seg.xlsx', index=True)
        yuanshi = pd.read_excel(f'{file_path}_pr_mP.xlsx')
        df = pd.read_excel(f'{file_path}_pr_changeSeg.xlsx')
        yuanshi = yuanshi.set_index('pr_number').join(df.set_index('pr_number'))
        yuanshi.to_excel(f'{file_path}_pr_mP_seg.xlsx', index=True)

